package com.vpen.iot.client.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Vpen
 * @date 2023/7/8 14:28
 */
@Slf4j
public class DelimiterEncoder extends MessageToByteEncoder<ByteBuf> {
    byte[] head;
    byte[] tail;

    public DelimiterEncoder(byte[] head,byte[] tail){
        this.head=head;
        this.tail=tail;
    }
    public DelimiterEncoder(){
        this.head= new byte[]{0x1f, 0x2f};
        this.tail= new byte[]{0x1f, 0x3f};
    }


    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ByteBuf in, ByteBuf out) {
            out.writeBytes(head);
            out.writeBytes(in);
            out.writeBytes(tail);
            log.debug("加头加尾巴：{}", ByteBufUtil.hexDump(out));
    }
}
